package com.leetcode;

import java.util.ArrayList;
import java.util.List;
import java.util.StringJoiner;

/**
 * 找到数组中消失的数字
 *
 * @author winson
 * @create 2022-04-14 15:21
 */
public class DisappearedNumbersSolution {

    /**
     * 采用原地标记
     * @param nums
     * @return
     */
    public List<Integer> findDisappearedNumbers(int[] nums) {
        List<Integer> ans = new ArrayList<>();
        int n = nums.length;
        for (int i = 0; i < nums.length; i++) {
            int num = nums[i];
            // 元素应在位置 num - 1;
            int index = (num - 1) % n;
            nums[index] = num + n;
        }
        for (int i = 0; i < n; i++) {
            if (nums[i] < n) {
                ans.add(i + 1);
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        int[] nums = new int[]{1,2,2,5,5,8,7,6};
        DisappearedNumbersSolution solution = new DisappearedNumbersSolution();
        List<Integer> ans = solution.findDisappearedNumbers(nums);
        for (Integer n : ans) {
            System.out.println(n);
        }
    }
}
